#include "ChangeNotifyThread.h"

#include <iostream>
#include <string>

using namespace std;

ChangeNotifyThread::ChangeNotifyThread(QObject *parent, QString dir) :
    QThread(parent),
    m_dir(dir)
{
}

void ChangeNotifyThread::run()
{
    if(m_handle == NULL)
	return;

    wstring _dirName = m_dir.toStdWString();

    cout << "monitoring " << _dirName.c_str() << endl;

    m_handle = FindFirstChangeNotification(
	  _dirName.c_str(),
	  TRUE,
	  FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_SIZE);

    std::cout << m_handle << " " << GetLastError() << std::endl;

    while(WaitForSingleObject(m_handle, INFINITE) == WAIT_OBJECT_0)
    {
	emit updateDir(m_dir);
	FindNextChangeNotification(m_handle);
    }
}

